package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.InventoryLocks;
import com.example.mall.dto.InventoryLocksDto;
import com.example.mall.mapper.InventoryLocksMapper;
import com.example.mall.query.InventoryLocksQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.IInventoryLocksService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 库存锁定记录表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-09
 */
@Service
public class InventoryLocksServiceImpl extends ServiceImpl<InventoryLocksMapper, InventoryLocks> implements IInventoryLocksService {

    @Autowired
    private InventoryLocksMapper inventoryLocksMapper;

    @Override
    public PageDTO<InventoryLocksDto> queryPage(InventoryLocksQuery inventoryLocksQuery) {
        if (inventoryLocksQuery == null) {
            inventoryLocksQuery = new InventoryLocksQuery();
        }
        Page<InventoryLocks> page = inventoryLocksQuery.toMpPageByDefaultCreateTimeAsc();
        Page<InventoryLocks> cartPage = lambdaQuery()
                .like(inventoryLocksQuery.getLockId()!= null, InventoryLocks::getLockId, inventoryLocksQuery.getLockId())
                .like(inventoryLocksQuery.getProductId()!= null, InventoryLocks::getProductId, inventoryLocksQuery.getProductId())
                .like(inventoryLocksQuery.getSpecId()!= null, InventoryLocks::getSpecId, inventoryLocksQuery.getSpecId())
                .like(inventoryLocksQuery.getOrderId()!= null, InventoryLocks::getOrderId, inventoryLocksQuery.getOrderId())
                .like(inventoryLocksQuery.getLockedStock()!= null, InventoryLocks::getLockedStock, inventoryLocksQuery.getLockedStock())
                .like(inventoryLocksQuery.getLockStatus()!= null, InventoryLocks::getLockStatus, inventoryLocksQuery.getLockStatus())
                .orderByDesc(InventoryLocks::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, InventoryLocksDto.class);
    }

    @Override
    public InventoryLocks queryInventoryLocksById(Long lockId) {
        AssertUtil.isNotNull(lockId, "库存锁定记录ID不能为空");
        return inventoryLocksMapper.selectById(lockId);
    }

    @Override
    public List<InventoryLocks> queryInventoryLocksList() {
        return this.list();
    }

    @Override
    public void insert(InventoryLocksDto inventoryLocksDto) {
        InventoryLocks inventoryLocks = new InventoryLocks();
        CopyUtils.copyProperties(inventoryLocksDto, inventoryLocks);
        inventoryLocks.setLockId(Long.valueOf(CodeGenerateUtils.generateUniqueId()));
        inventoryLocks.setCreateDate(new Date());
        inventoryLocks.setUpdateDate(new Date());
        inventoryLocks.setLockStatus(BaseConstants.EFFECTIVE_STATUS);
        inventoryLocksMapper.insert(inventoryLocks);
    }

    @Override
    public void update(InventoryLocksDto inventoryLocksDto) {
        AssertUtil.isNotNull(inventoryLocksDto.getLockId(), "库存锁定记录ID不能为空");
        InventoryLocks inventoryLocks = inventoryLocksMapper.selectById(inventoryLocksDto.getLockId());
        AssertUtil.isNotNull(inventoryLocks, "库存锁定记录不存在");
        CopyUtils.copyProperties(inventoryLocksDto, inventoryLocks);
        inventoryLocks.setUpdateDate(new Date());
        inventoryLocksMapper.updateById(inventoryLocks);
    }

    @Override
    public void delete(Long lockId) {
        AssertUtil.isNotNull(lockId, "库存锁定记录ID不能为空");
        InventoryLocks inventoryLocks = inventoryLocksMapper.selectById(lockId);
        AssertUtil.isNotNull(inventoryLocks, "库存锁定记录不存在");
        inventoryLocks.setLockStatus(BaseConstants.LOSE_STATUS);
        inventoryLocks.setUpdateDate(new Date());
        inventoryLocksMapper.updateById(inventoryLocks);
    }

    @Override
    public void deleteBatch(List<Long> lockIds) {
        AssertUtil.isNotNull(lockIds, "库存锁定记录ID列表不能为空");
        List<InventoryLocks> inventoryLocks = inventoryLocksMapper.selectBatchIds(lockIds);
        AssertUtil.isNotNull(inventoryLocks, "库存锁定记录不存在");
        for (InventoryLocks inventoryLock : inventoryLocks) {
            inventoryLock.setLockStatus(BaseConstants.LOSE_STATUS);
            inventoryLock.setUpdateDate(new Date());
            inventoryLocksMapper.updateById(inventoryLock);
        }
    }
}
